package com.yijiu.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.yijiu.resource.Project;
import com.yijiu.trb.R;

public class SuggestProjectAdapter extends MyBaseAdapter
{
	private Context context;
	private View progressBar;
	
	public SuggestProjectAdapter(Context context, List<Project> projectList)
	{
		this.context = context;
		this.list = projectList;
	}
	@Override
	public int getCount()
	{
		if (isShowProgress)
		{
			return list.size() + 1;
		}
		return list.size();
	}
	@Override
	public Object getItem(int position)
	{
		if (isShowProgress)
		{
			return null;
		}
		return list.get(position);
	}
	@Override
	public long getItemId(int position)
	{
		return position;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		if (position == list.size())
		{         
			if (progressBar == null)
			{
				progressBar = LayoutInflater.from(context).inflate(R.layout.load_next_page_layout, null);
			}
			return progressBar;
		} 
		else
		{
			convertView = LayoutInflater.from(context).inflate(R.layout.suggest_project_adapter_layout, null);
			TextView projectName = (TextView) convertView.findViewById(R.id.tv_suggest_project_adapter_project_name);
			TextView status = (TextView) convertView.findViewById(R.id.tv_suggest_project_adapter_status);
			TextView needMoney = (TextView) convertView.findViewById(R.id.tv_suggest_project_adapter_need_money);
			TextView companyName = (TextView) convertView.findViewById(R.id.tv_suggest_project_adapter_company_name);
			TextView yearGet = (TextView) convertView.findViewById(R.id.tv_suggest_project_adapter_year_get);
			TextView moneyTime = (TextView) convertView.findViewById(R.id.tv_suggest_project_adapter_money_time);
			TextView createTime = (TextView) convertView.findViewById(R.id.tv_suggest_project_adapter_create_time);
			TextView endTime = (TextView) convertView.findViewById(R.id.tv_suggest_project_adapter_end_time);
			TextView moneyStatus = (TextView) convertView.findViewById(R.id.tv_suggest_project_adapter_money_status);
			TextView progressText = (TextView) convertView.findViewById(R.id.tv_suggest_project_adapter_progress);
			ProgressBar progressBar = (ProgressBar) convertView.findViewById(R.id.pb_suggest_project_adapter);
			Project project = (Project) list.get(position);
			projectName.setText(project.tradeName);
			int statusNum = project.tradeStatus;
			String statusStr = "";
			if (statusNum == 1)
			{
				statusStr = "募集中";
				endTime.setText("截止时间  " + project.deadline);
			}
			else if (statusNum == 2)
			{
				statusStr = "未到期";
				endTime.setText("成立时间  " + project.deadline);
			}
			else if (statusNum == 3 || statusNum == 7)
			{
				statusStr = "已还款";
				endTime.setText("还款时间  " + project.deadline);
			}
			else if (statusNum == 6)
			{
				statusStr = "审核中";
				endTime.setText("完成时间  " + project.deadline);
			}
			else if (statusNum == 8)
			{
				statusStr = "还款中";
				endTime.setText("成立时间  " + project.deadline);
			}
			status.setText(statusStr);
			needMoney.setText("需求金额  ￥" + project.tradeAmount);
			companyName.setText("担保机构：" + project.guaranteeName);
			yearGet.setText("年化收益  " + project.tradeAnnualRate);
			moneyTime.setText("融资期限  " + project.timePeriod);
			createTime.setText("起投时间  " + project.investStartTime);
			String moneyStr = project.tradeAmount;
			moneyStr = moneyStr.replace(".00", "");
			moneyStr = moneyStr.replace(",", "");
			if (statusNum == 2)
			{
				moneyStatus.setText("成立金额  ￥" + project.tradeLoanedAmount);
			}
			else
			{
				moneyStatus.setText("已融资" + (double) ((Long.parseLong(moneyStr) - project.requiredAmount * 10000.0) / 10000)
						+ "万元，还需" + project.requiredAmount + "万元");
			}
			progressText.setText(project.persontage);
			String strRate = project.persontage.replace("%", "");
			float numRate = Float.valueOf(strRate);
			progressBar.setProgress((int) numRate);
			if (position >= list.size() - 1 && listener != null)
			{
				listener.onFoot();
			}
			return convertView;
		}
	}
}
